package kargo.suratkargokargotakip;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class WebServiceMethods {
	
	public static String[] RequestXml(String url, String methodName,
			HashMap<String, Object> parameters, final String startTagName,
			final Boolean firstLevel) {
		String envelope = "<?xml version=\"1.0\" encoding=\"iso-8859-9\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
				+ "soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">"
				+ "<soap:Body>";
		envelope += "<" + methodName + " xmlns=\"http://tempuri.org/\">";

		if (parameters != null) {
			for (HashMap.Entry<String, Object> entry : parameters.entrySet()) {
				envelope += "<" + entry.getKey() + ">" + entry.getValue()
						+ "</" + entry.getKey() + ">";
			}
		}
		envelope += "</" + methodName + ">";
		envelope += "</soap:Body>" + "</soap:Envelope>";
		// String url = "http://webservices.suratkargo.com.tr/services.asmx";
		String soapAction = "http://tempuri.org/" + methodName;
			String[] params= {soapAction,envelope};
			
			return params;
	}
	
	public static ArrayList<HashMap<String, String>> FillArrayForSpinner( String str, String startTagName,
			Boolean firstLevel) throws ParserConfigurationException,
			SAXException, IOException {
		ArrayList<HashMap<String, String>> array = null;
		if (str != null) {

			String sMethodBaslangic = "";
			if (firstLevel)
				sMethodBaslangic = "<" + startTagName + ">";
			else
				sMethodBaslangic = "<" + startTagName + " xmlns=\"\">";
			String sMethodBitis = "</" + startTagName + ">";
			int artiBaslangic = sMethodBaslangic.indexOf(">");
			artiBaslangic++;
			int baslangic = str.indexOf(sMethodBaslangic);
			baslangic += artiBaslangic;
			str = str.substring(baslangic);
			int bitis = str.indexOf(sMethodBitis);
			str = str.substring(0, bitis);
			str = str + sMethodBitis;
			str = new StringBuffer(str).insert(0, "<" + startTagName + ">")
					.toString();

			byte[] bytes = str.getBytes();
			ByteArrayInputStream bInputStream = new ByteArrayInputStream(bytes);

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			org.w3c.dom.Document doc = docBuilder.parse(bInputStream);
			org.w3c.dom.Element element = doc.getDocumentElement();
			NodeList nl = element.getChildNodes();
			int rowIndex = nl.getLength();
			array = new ArrayList<HashMap<String, String>>();
			
			if (firstLevel) {
				array.add(new HashMap<String, String>());
				for (int j = 0; j < rowIndex; j++) {
					Node node = nl.item(j);
					if (node.getLastChild() != null)
						{array.get(0).put(node.getNodeName(),
								node.getLastChild().getNodeValue());}
					else if(rowIndex==1)
					{
						str = str.substring(artiBaslangic);
						str = str.substring(0, bitis);
						array.get(0).put("sonuc", str.toString());}
				}
			} else {

				for (int i = 0; i < rowIndex; i++) {
					NodeList nlChildren = nl.item(i).getChildNodes();
					array.add(new HashMap<String, String>());
					for (int j = 0; j < nlChildren.getLength(); j++) {
						Node node = nlChildren.item(j);
						if (node.getChildNodes().item(0) != null)
							array.get(i).put(
									node.getNodeName(),
									(node.getChildNodes().item(0)
											.getNodeValue()));
					}

				}
			}
			
			
			
		}
		return array;
	}

}
